package com.ruoyi.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.IceResult;
import com.ruoyi.contract.service.IceResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/contract/ice-result")
public class IceResultController extends BaseController {

    @Autowired
    IceResultService iceResultService;

    @GetMapping("/list")
    public TableDataInfo list(IceResult result){
        startPage();
        QueryWrapper<IceResult> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda()
                .like(StringUtils.isNotEmpty(result.getResultName()),IceResult::getResultName,result.getResultName())
                .like(StringUtils.isNotEmpty(result.getDatabaseName()),IceResult::getDatabaseName,result.getDatabaseName())
                .orderByDesc(IceResult::getId);

        List<IceResult> list = iceResultService.list(queryWrapper);
        return getDataTable(list);
    }

    @GetMapping("/getOne/{id}")
    public AjaxResult getOne(@PathVariable Long id){
        return AjaxResult.success(iceResultService.getById(id));
    }

    @GetMapping("/label")
    public AjaxResult label(){
        return AjaxResult.success(iceResultService.list());
    }

    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('contract:ice-result:add')")
    @Log(title = "新增计费字段", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody IceResult result){

        try {
            return iceResultService.insert(result);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }

    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('contract:ice-result:update')")
    @Log(title = "修改计费字段", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody IceResult result){
        try {
            return iceResultService.update(result);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermi('contract:ice-result:remove')")
    @Log(title = "删除计费字段", businessType = BusinessType.DELETE)
    public AjaxResult delete(@PathVariable Long[] id){
        return iceResultService.removeByIds(Arrays.asList(id))?AjaxResult.success():AjaxResult.error();
    }

}
